package com.fighYo.edu.service.impl;

import com.fighYo.edu.entity.EduChapter;
import com.fighYo.edu.entity.EduCourse;
import com.fighYo.edu.entity.EduCourseDescription;
import com.fighYo.edu.entity.vo.CourseInfoVo;
import com.fighYo.edu.entity.vo.CoursePublishVo;
import com.fighYo.edu.mapper.EduCourseMapper;
import com.fighYo.edu.service.EduChapterService;
import com.fighYo.edu.service.EduCourseDescriptionService;
import com.fighYo.edu.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fighYo.edu.service.EduVideoService;
import com.fighYo.servicebase.config.handler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author fighYo
 * @since 2021-08-24
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;
    @Autowired
    private EduChapterService eduChapterService;
    @Autowired
    private EduVideoService eduVideoService;

    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if(insert <= 0) {
            throw new GuliException(20001, "添加课程失败");
        }

        String id = eduCourse.getId();

        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(id);
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescriptionService.save(eduCourseDescription);
        return id;
    }

    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        EduCourse eduCourse = baseMapper.selectById(courseId);
        if(eduCourse == null) {
            throw new GuliException(20001, "数据不存在");
        }

        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse, courseInfoVo);

        EduCourseDescription eduCourseDescription = eduCourseDescriptionService.getById(courseId);
        if(eduCourseDescription != null) {
            courseInfoVo.setDescription(eduCourseDescription.getDescription());
        }

        return courseInfoVo;
    }

    @Override
    public boolean updateCourseInfo(CourseInfoVo courseInfo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfo, eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if(update <= 0) {
            throw new GuliException(20001, "修改课程失败");
        }

        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfo, eduCourseDescription);
        eduCourseDescriptionService.updateById(eduCourseDescription);
        return true;
    }

    @Override
    public CoursePublishVo getPublishCourseInfo(String id) {
        CoursePublishVo courseInfo = baseMapper.getPublishCourseInfo(id);
        return courseInfo;
    }

    @Override
    public boolean publishCourseById(String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus(EduCourse.COURSE_NORMAL);
        int update = baseMapper.updateById(eduCourse);
        return update > 0;
    }

    @Override
    public boolean removeCourseByCourseId(String courseId) {
        eduChapterService.removeChapterByCourseId(courseId);
        eduVideoService.removeVideoByCourseId(courseId);

        int delete = baseMapper.deleteById(courseId);
        return delete > 0;
    }
}
